package com.smart.system.domain;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.smart.common.exception.ServiceException;
import com.smart.common.utils.DateUtils;
import com.smart.common.utils.StringUtils;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.smart.common.annotation.Excel;
import com.smart.common.core.domain.BaseEntity;

import java.text.ParseException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 排班对象 working_schedule
 * 
 * @author smart
 * @date 2024-09-18
 */
@Data
@Builder
@TableName("working_schedule")
@EqualsAndHashCode(callSuper = true)
@AllArgsConstructor
@NoArgsConstructor
public class WorkingSchedule extends BaseEntity
{
    private static final long serialVersionUID = 1L;

    /** 主键标识 */
    @Schema(description ="${comment}")
    @TableField(value = "id",exist = true)
    @TableId(type = IdType.AUTO)
    private Integer id;

    /** 租户号 */
    @Excel(name = "租户号")
    @Schema(description ="租户号")
    @TableField(value = "dept_id",exist = true)
    private String deptId;

    /** 乐观锁 */
    @Excel(name = "乐观锁")
    @Schema(description ="乐观锁")
    @TableField(value = "revision",exist = true)
    private String revision;

    /** 是否删除;0正常1删除 */
    @Schema(description ="乐观锁")
    @TableField(value = "del_flag",exist = true)
    private String delFlag;

    /** 是否启用;0正常1冻结 */
    @Excel(name = "是否启用;0正常1冻结")
    @Schema(description ="是否启用;0正常1冻结")
    @TableField(value = "status",exist = true)
    private String status;

    /** 下班时间 */
    @Excel(name = "下班考勤开始时间")
    @Schema(description ="下班考勤开始时间")
    @TableField(value = "work_end_up_time",exist = true)
    private String workEndUpTime;
    @Excel(name = "下班考勤结束时间")
    @Schema(description ="下班考勤结束时间")
    @TableField(value = "work_end_down_time",exist = true)
    private String workEndDownTime;

    /** 上班时间 */
    @Excel(name = "上班考勤开始时间")
    @Schema(description ="上班考勤开始时间")
    @TableField(value = "work_start_up_time",exist = true)
    private String workStartUpTime;
    @Excel(name = "上班考勤结束时间")
    @Schema(description ="上班考勤结束时间")
    @TableField(value = "work_start_down_time",exist = true)
    private String workStartDownTime;

    /** 日期 */
    @Excel(name = "工作日,逗号隔开")
    @Schema(description ="日期")
    @TableField(value = "workday",exist = true)
    private String workday;

    @Excel(name = "")
    @Schema(description ="打卡距离")
    @TableField(value = "distance",exist = true)
    private Integer distance;

    public List<Integer> getArray(){
        if (StringUtils.isEmpty(this.workday)) {
            return Collections.emptyList();
        }
        if (this.workday.contains(",")) {
            String[] split = this.workday.split(",");
            return Arrays.stream(split).map(Integer::parseInt).collect(Collectors.toList());
        }
        return null;
    }

    /**
     * 判断当前时间是否上班时间
     * 上班时间=上班打卡结束时间-下班打卡开始时间
     * @return true 在上班时间内,false不在上班时间内
     */
    public boolean isWorkingUpTime() throws ParseException {
        //获取当前时间,并拼接完整的时间
        String formatDate = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,new Date());
        Date startTime = DateUtils.parseDate(formatDate + " " + this.getWorkStartDownTime(), DateUtils.YYYY_MM_DD_HH_MM_SS);
        Date downTime = DateUtils.parseDate(formatDate + " " + this.getWorkEndUpTime(), DateUtils.YYYY_MM_DD_HH_MM_SS);

        //判断当前时间是否在开始时间和结束时间的区间之内
        return DateUtils.getNowDate().compareTo(startTime) >= 0 && DateUtils.getNowDate().compareTo(downTime) <= 0;
    }

    //判断是否为工作日
    public boolean isWorkDay() {
        List<Integer> array = getArray();
        if (array==null||array.isEmpty()) {
            return false;
        }
        return array.contains(DateUtils.getNowDate().getDay());
    }
}
